package com.hotec.PROJECT_NAME.component.basic;

import com.gemcap.framework.common.data.dto.UtilsDTO;
import com.gemcap.framework.common.data.dto.dtocallbackhandler.DTOCallbackHandlerProxy;
import com.gemcap.framework.common.data.dto.dtocallbackhandler.DTOCallbackHandlerWithNumber;
import com.gemcap.framework.common.data.dto.support.DTOCallbackHandler;
import com.gemcap.framework.common.data.dto.support.EmptyDTOCallbackHandler;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.hotec.PROJECT_NAME.component.dtohandler.callbackhandler.DTOCallbackHandlerWithAttachment;
import com.hotec.PROJECT_NAME.component.dtohandler.callbackhandler.DTOCallbackHandlerWithDic;
import com.hotec.PROJECT_NAME.component.dtohandler.callbackhandler.DTOCallbackHandlerWithEnum;
import com.hotec.PROJECT_NAME.modules.sys.service.SysAttachmentService;
import com.hotec.PROJECT_NAME.modules.sys.service.SysDictService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DecimalFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * @author
 */
public class BaseController extends BaseBase implements InitializingBean {
    @Autowired
    SysDictService sysDictService;
    @Autowired
    SysAttachmentService sysAttachmentService;

    protected DecimalFormat defaultPercentFormat = new DecimalFormat("0.00%");

    protected DecimalFormat defaultDecimalFormat = new DecimalFormat("0.00");


    @Getter
    @Setter
    public DTOCallbackHandlerProxy dtoCallbackHandlerProxy = new DTOCallbackHandlerProxy();

    private DTOCallbackHandlerWithDic dtoCallbackHandlerWithDic = new DTOCallbackHandlerWithDic();

    private DTOCallbackHandlerWithNumber dtoCallbackHandlerWithNumber = new DTOCallbackHandlerWithNumber();
    private DTOCallbackHandlerWithEnum dtoCallbackHandlerWithEnum= new DTOCallbackHandlerWithEnum();

    private DTOCallbackHandlerWithAttachment dtoCallbackHandlerWithAttachment = new DTOCallbackHandlerWithAttachment();

    @Override
    public void afterPropertiesSet() {
        //添加对字典的处理
        dtoCallbackHandlerWithDic.setSysDictService(sysDictService);
        //添加对附件的处理
        dtoCallbackHandlerWithAttachment.setSysAttachmentService(sysAttachmentService);
        //添加对字典项的处理
        dtoCallbackHandlerProxy.addDTOCallbackHandlerTask(dtoCallbackHandlerWithDic);
        //添加对数字的处理
        dtoCallbackHandlerProxy.addDTOCallbackHandlerTask(dtoCallbackHandlerWithNumber);
        dtoCallbackHandlerProxy.addDTOCallbackHandlerTask(dtoCallbackHandlerWithEnum);
        //添加附件自动处理类
        dtoCallbackHandlerProxy.addDTOCallbackHandlerTask(dtoCallbackHandlerWithAttachment);
    }

    /**
     * 添加类属性和父字典项code的映射
     *
     * @param property
     * @param dictionaryCode
     */
    public void addDictionaryProperty(String property, String dictionaryCode) {
        //添加实体属性与父节点code的映射
        dtoCallbackHandlerWithDic.getPropertyDicMap().put(property, dictionaryCode);
    }

    /**
     * 当前controller处理dto默认类
     *
     * @return
     */
    public DTOCallbackHandler getDefaultDTOCallbackHandler() {
        return new EmptyDTOCallbackHandler();
    }

    /**
     * 添加类的数字属性和格式化方式的映射
     *
     * @param property
     * @param format
     */
    public void addNumberProperty(String property, String format) {
        dtoCallbackHandlerWithNumber.addNumberProperty(property, format, null);
    }

    /**
     * 添加类的数字属性和格式化方式的映射
     *
     * @param property
     * @param format
     * @param predicate
     */
    public void addNumberProperty(String property, String format, Predicate<Map<String, Object>> predicate) {
        dtoCallbackHandlerWithNumber.addNumberProperty(property, format, predicate);
    }


    public JsonContent success(String msg, Object data, boolean dtoHandle) {
        dtoCallbackHandlerProxy.setTargetDTOCallbackHandler(getDefaultDTOCallbackHandler());
        //是否需要dto处理
        if (dtoHandle) {
            if (data instanceof PageInfo) {
                PageInfo dataPage = (PageInfo) data;
                if (dataPage.getList() != null && dataPage.getList().size() > 0) {
                    List<Map<String, Object>> dtoResults = UtilsDTO.toDTOExcludeFields(dataPage.getList(), null, getDtoCallbackHandlerProxy());
                    PageInfo newPageInfo = PageInfo.of(dtoResults, dataPage.getNavigatePages());
                    newPageInfo.setPageNum(dataPage.getPageNum());
                    newPageInfo.setPages(dataPage.getPages());
                    newPageInfo.setPageSize(dataPage.getPageSize());
                    newPageInfo.setTotal(dataPage.getTotal());
                    data = newPageInfo;
                }
            } else if (data instanceof Collection) {

                data = UtilsDTO.toDTOExcludeFields(Lists.newArrayList((Collection) data), null, getDtoCallbackHandlerProxy());
            } else {
                data = UtilsDTO.toDTOExcludeFields(data, null, getDtoCallbackHandlerProxy());
            }
//            System.out.println("dto处理结果：" + JSON.toJSONString(data, true));
        }
        return JsonContent.success(data).setMessage(msg);
    }

    /**
     * 返回成功数据，添加提示，默认走handler
     *
     * @param msg
     * @return
     */
    public JsonContent success(String msg, Object data) {
        return this.success(msg, data, true);
    }

    /**
     * 返回成功数据，添加提示，没有数据
     *
     * @param msg
     * @return
     */
    public JsonContent success(String msg) {
        return this.success(msg, null);
    }

    /**
     * 返回成功数据,没有数据，默认提示
     *
     * @return
     */
    public JsonContent success() {
        return this.success("操作成功！", null);
    }

    /**
     * 返回成功数据，默认提示,默认handler
     *
     * @return
     */
    public JsonContent success(Object data) {
        return this.success("操作成功！", data, true);
    }




}
